//
//  LXThreeBtnView.m
//  灵秀
//
//  Created by 李田 on 17/3/18.
//  Copyright © 2017年 tianyao. All rights reserved.
//

#import "LXThreeBtnView.h"

@interface LXThreeBtnView ()
// 小黄条
@property (nonatomic, weak) UIView  *yellowLine;
// 选中的按钮
@property(nonatomic, weak)UIButton *selectedBtn;
@end

@implementation LXThreeBtnView {
    NSMutableArray <UIButton *>*_btnsArray;
}
#pragma mark - 3.0点击按钮 切换小黄条
- (void)btnClick: (UIButton *)sender {
    
    // 获取角标
    NSInteger index = [_btnsArray indexOfObject:sender];
    // 修改小黄条的布局
    [_yellowLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_btnsArray[0]).offset(index * _btnsArray[0].bounds.size.width);
    }];
    // 添加动画
    [UIView animateWithDuration:.3 animations:^{
        [self layoutIfNeeded];
    }];
    
    // 2.0 给控制器的 发送消息
    if ([self.delegate respondsToSelector:@selector(threeBtnView:withIndex:)]) {
        [self.delegate threeBtnView:self withIndex:index];
    }
    
    // 3.0 设置按钮的选中状态
    self.selectedBtn.selected = NO;
    sender.selected = YES;
    self.selectedBtn = sender;
}

#pragma mark - 2.0 接收手动滚动的偏移量
- (void)setOffsetX:(CGFloat)offsetX {
    
    _offsetX = offsetX;
    // 1.0修改小黄条的布局
    [_yellowLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_btnsArray[0]).offset(offsetX);
    }];
    // 加个动画
    [UIView animateWithDuration:.3 animations:^{
        [self layoutIfNeeded];
    }];
    // 2.0 设置按钮的选中状态
    // 计算角标
    NSInteger index = offsetX / _btnsArray[0].bounds.size.width + 0.5;
    UIButton *btn = _btnsArray[index];
    self.selectedBtn.selected = NO;
    btn.selected = YES;
    self.selectedBtn = btn;
}

#pragma mark - 1.0 搭建界面
- (void)setupUI {
    
    self.backgroundColor = [UIColor whiteColor];
    
    //MARK:1.创建添加3个按钮
    
    //1.文字数组
    NSArray <NSString *>*titleArray = @[@"点菜",@"评价",@"商家"];
    
    //2.循环创建 btn
    _btnsArray = [NSMutableArray array];
    [titleArray enumerateObjectsUsingBlock:^(NSString * _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIButton *btn = [UIButton cz_textButton:title fontSize:14 normalColor:[UIColor cz_colorWithHex:0x555555] selectedColor:[UIColor redColor]];
        // 添加按钮点击事件
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:btn];
        // 设置默认选中
        if (idx == 0) {
            
            btn.selected = YES;
            self.selectedBtn = btn;
        }
        
        [_btnsArray addObject:btn];
    }];
    
    //4.小黄条
    UIView *yellowLine = [[UIView alloc]init];
    yellowLine.backgroundColor = [UIColor cz_colorWithHex:0xffd900];
    [self addSubview:yellowLine];
    
    // 关联
    _yellowLine = yellowLine;

    //3.布局
    [_btnsArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [_btnsArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
    }];
    
    [yellowLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.bottom.width.equalTo(_btnsArray[0]);
        make.height.mas_equalTo(4);
    }];
}

@end
